#ifndef PYTHONAPI_H_
#define PYTHONAPI_H_
#include <Python.h>
#include <wx/wx.h>
#include <map>

class PythonMenuCallbackHandler
{
	std::map<int, PyObject*> m_callbacks;
	int m_min;
	int m_max;
public:
	PythonMenuCallbackHandler(){
		m_min = 0;
		m_max = 0;
	}
	PythonMenuCallbackHandler(int min, int max){
		m_min = min;
		m_max = max;
	}
	int GetFreeId(){
		for(int i=m_min; i<=m_max; i++){
			if(m_callbacks.find(i) == m_callbacks.end()){
				return i;
			}
		}
		return -1;
	}
	// returns allocated id and -1 on failure. Increases reference count.
	int AddObject(PyObject *obj){
		if(obj==NULL) return -1;
		int id = GetFreeId();
		if(id==-1) return -1;
		Py_INCREF(obj);
		m_callbacks[id] = obj;
		return id;
	}
	PyObject * GetObject(int i)
	{
		if(m_callbacks.find(i) == m_callbacks.end()){
			return NULL;
		}
		return m_callbacks[i];
	}
};

class PythonApi
{
	PythonMenuCallbackHandler m_command_handler;
	PythonMenuCallbackHandler m_check_handler;
	PyObject *m_char_handler;
	PyObject *m_special_handler;
public:
	PythonApi();
	~PythonApi();
	
	bool ExecFile(wxString path);

	// Increases reference count, returns id.
	// func(menupath, itemname)
	int AddMenuCommand(PyObject *func);
	void ExecMenuCommand(int id);
	// Increases reference count, returns id.
	// func(menupath, itemname, state)
	int AddMenuCheck(PyObject *func);
	void ExecMenuCheck(int id, bool checked);
};

#endif

